{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  黄鹏辉\n",
    "+ 学号  0161918"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "夜已深了\n",
      "雨水的气味\n",
      "渐渐蔓延\n",
      "缓缓时钟习惯性失眠\n",
      "墙面上摇晃的树影\n",
      "有一点倔强独特的美\n",
      "有些陌生\n",
      "有些熟悉\n",
      "似有若无的爱情\n",
      "\n",
      "你就像烟火的美丽\n",
      "那么美丽\n",
      "轻划过无人的天际\n",
      "曾经交换过的秘密\n",
      "紧紧埋藏在心底\n",
      "\n",
      "夜已深了\n",
      "雨水的气味\n",
      "渐渐蔓延\n",
      "缓缓时钟习惯性失眠\n",
      "墙面上摇晃的树影\n",
      "有一点倔强独特的美\n",
      "有些陌生\n",
      "有些熟悉\n",
      "似有若无的爱情\n",
      "\n",
      "你就像烟火的美丽\n",
      "那么美丽\n",
      "轻划过无人的天际\n",
      "曾经交换过的秘密\n",
      "紧紧埋藏在心底\n",
      "\n",
      "你就像烟火的美丽\n",
      "那么美丽\n",
      "轻划过无人的天际\n",
      "曾经交换过的秘密\n",
      "紧紧埋藏在心底\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def chorus():\n",
    "    print(\"你就像烟火的美丽\\n那么美丽\\n轻划过无人的天际\\n曾经交换过的秘密\\n紧紧埋藏在心底\\n\")\n",
    "A=(\"夜已深了\\n雨水的气味\\n渐渐蔓延\\n缓缓时钟习惯性失眠\\n墙面上摇晃的树影\\n有一点倔强独特的美\\n有些陌生\\n有些熟悉\\n似有若无的爱情\\n\")\n",
    "print(A)\n",
    "chorus()\n",
    "print(A)\n",
    "chorus()\n",
    "chorus()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共输入了 13 个数\n",
      "他们的均值为 5.615384615384615\n",
      "他们的方差是 77.07692307692308\n",
      "一共输入了 17 个数\n",
      "他们的均值为 1.0\n",
      "他们的方差是 0.0\n"
     ]
    }
   ],
   "source": [
    "def hanshu(*args):\n",
    "    he=0\n",
    "    junzhi=0\n",
    "    fangcha=0\n",
    "    n=0\n",
    "    for value in args:\n",
    "        he=he+value\n",
    "        n=n+1\n",
    "    junzhi=he/n\n",
    "    for value in args:\n",
    "        fangcha=fangcha+(value-junzhi)*(value-junzhi)\n",
    "    print(\"一共输入了\",n,\"个数\")\n",
    "    print(\"他们的均值为\",junzhi)\n",
    "    print(\"他们的方差是\",fangcha)\n",
    "hanshu(1,2,5,9,8,7,4,5,3,6,8,7,8)\n",
    "hanshu(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共输入了 13 个数\n",
      "他们的均值为 5.615384615384615\n",
      "他们的标准差是 8.779346392353082\n",
      "一共输入了 17 个数\n",
      "他们的均值为 1.0\n",
      "他们的方差是 0.0\n"
     ]
    }
   ],
   "source": [
    "def hanshu(*args,var=None):\n",
    "    he=0\n",
    "    junzhi=0\n",
    "    biaozhuncha=0\n",
    "    fangcha=0\n",
    "    n=0\n",
    "    for value in args:\n",
    "        he=he+value\n",
    "        n=n+1\n",
    "    junzhi=he/n\n",
    "    for value in args:\n",
    "        fangcha=fangcha+(value-junzhi)*(value-junzhi)\n",
    "        biaozhuncha=fangcha**(1/2)\n",
    "    print(\"一共输入了\",n,\"个数\")\n",
    "    print(\"他们的均值为\",junzhi)\n",
    "    if var==1:\n",
    "        print(\"他们的方差是\",fangcha)\n",
    "    if var==0:\n",
    "        print(\"他们的标准差是\",biaozhuncha)\n",
    "hanshu(1,2,5,9,8,7,4,5,3,6,8,7,8,var=0)\n",
    "hanshu(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,var=1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共输入了 13 个数\n",
      "他们的均值为 5.615384615384615\n",
      "他们的标准差是 2.434952583283756\n",
      "最大值为 9\n",
      "最小值为 1\n",
      "\n",
      "\n",
      "一共输入了 17 个数\n",
      "他们的均值为 1.0\n",
      "他们的方差是 0.0\n",
      "最大值为 1\n",
      "\n",
      "\n",
      "一共输入了 12 个数\n",
      "他们的均值为 36.166666666666664\n",
      "他们的标准差是 34.58042156416772\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "def hanshu(*args,var=None,biaozhunwu=None,maxn=None,minu=None):\n",
    "    he=0\n",
    "    junzhi=0\n",
    "    biaozhuncha=0\n",
    "    fangcha=0\n",
    "    n=0\n",
    "    for value in args:\n",
    "        he=he+value\n",
    "        n=n+1\n",
    "    junzhi=he/n\n",
    "    for value in args:\n",
    "        fangcha=fangcha+(value-junzhi)*(value-junzhi)\n",
    "    fangcha=fangcha/n    \n",
    "    biaozhuncha=fangcha**(1/2)\n",
    "    print(\"一共输入了\",n,\"个数\")\n",
    "    print(\"他们的均值为\",junzhi)\n",
    "    if var==1:\n",
    "        print(\"他们的方差是\",fangcha)\n",
    "    if var==0:\n",
    "        print(\"他们的标准差是\",biaozhuncha)\n",
    "    if biaozhunwu:\n",
    "        print(\"标准误为\",std/numpy.sqrt(n))\n",
    "    if maxn:\n",
    "        print(\"最大值为\",max(args))\n",
    "    if minu:\n",
    "        print(\"最小值为\",min(args))\n",
    "    print(\"\\n\")\n",
    "\n",
    "hanshu(1,2,5,9,8,7,4,5,3,6,8,7,8,var=0,biaozhunwu=0,maxn=1,minu=1)\n",
    "hanshu(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,var=1,maxn=1,minu=0)\n",
    "hanshu(1,2,5,8,7,4,55,66,99,88,44,55,var=0,maxn=0,minu=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
